package cn.itcast.wanxinp2p.depository.service.impl;

import cn.itcast.wanxinp2p.api.depository.model.ConsumerRequest;
import cn.itcast.wanxinp2p.api.depository.model.GatewayRequest;
import cn.itcast.wanxinp2p.common.domain.StatusCode;
import cn.itcast.wanxinp2p.common.util.EncryptUtil;
import cn.itcast.wanxinp2p.common.util.RSAUtil;
import cn.itcast.wanxinp2p.depository.common.constant.DepositoryRequestTypeCode;
import cn.itcast.wanxinp2p.depository.entity.DepositoryRecord;
import cn.itcast.wanxinp2p.depository.mapper.DepositoryRecordMapper;
import cn.itcast.wanxinp2p.depository.service.ConfigService;
import cn.itcast.wanxinp2p.depository.service.DepositoryRecordService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DepositoryRecordServiceImpl extends ServiceImpl<DepositoryRecordMapper, DepositoryRecord> implements DepositoryRecordService {

    @Autowired
    private ConfigService configService;

    @Override
    public GatewayRequest createConsumer(ConsumerRequest consumerRequest) {
        //1.保存交易记录
        saveDepositoryRecord(consumerRequest);

        //2生成签名数据

        String reqData = JSON.toJSONString(consumerRequest);

        //2.1签名
        String sign = RSAUtil.sign(reqData, configService.getP2pPrivateKey(), "utf-8");

        //3。构建返回数据
        GatewayRequest gatewayRequest = new GatewayRequest();
        gatewayRequest.setDepositoryUrl(configService.getDepositoryUrl());
        //传递数据时候需要进行Base64编码
        gatewayRequest.setReqData(EncryptUtil.encodeURL(EncryptUtil.encodeUTF8StringBase64(sign)));
        gatewayRequest.setServiceName("PERSONAL_REGISTER");
        gatewayRequest.setPlatformNo(configService.getP2pCode());
        gatewayRequest.setSignature(EncryptUtil.encodeURL(sign));

        return gatewayRequest;
    }

    /**
     * 更新状态
     *
     * @param requestNo      请求流水号
     * @param requestsStatus 请求状态
     * @return
     */
    @Override
    public Boolean modifyRequestStatus(String requestNo, Integer requestsStatus) {
        //使用mp 提供的链式调用方式处理业务
        this.update(Wrappers.<DepositoryRecord>lambdaUpdate()
                .eq(DepositoryRecord::getRequestNo, requestNo)
                .set(DepositoryRecord::getRequestStatus,requestsStatus)
                .set(DepositoryRecord::getConfirmDate, LocalDateTime.now())
        );

        return null;
    }


    //根据用户开户信息保存交易记录
    private void saveDepositoryRecord(ConsumerRequest consumerRequest) {

        DepositoryRecord depositoryRecord = new DepositoryRecord();
        //补充请求类型，开户
        depositoryRecord.setRequestNo(consumerRequest.getRequestNo());
        depositoryRecord.setRequestType(DepositoryRequestTypeCode.CONSUMER_CREATE.getCode());
        //业务实体类型，业务实体为客户
        depositoryRecord.setObjectType("Consumer");
        //开户状态设置为位同步
        depositoryRecord.setRequestStatus(StatusCode.STATUS_OUT.getCode());
        depositoryRecord.setObjectId(consumerRequest.getId());
        this.save(depositoryRecord);
    }
}
